//
//  PywValidProblemViewController.m
//  PywSdk
//
//  Created by smallwave on 16/8/18.
//  Copyright © 2016年 zero. All rights reserved.
//

#import "PywValidProblemViewController.h"
#import "PywPayPwdFooterView.h"
#import "PywValidProblemHeaderView.h"
#import "PywPayPwdSetProblemViewCell.h"
#import "PywMasonry.h"
#import "PywResetNewTradePwdController.h"

@interface PywValidProblemViewController ()<UITableViewDelegate, UITableViewDataSource, PywValidProblemViewDelegate, PywPayPwdFooterViewDelegate>

@property (nonatomic, strong) UITableView *tableView;
@property (nonatomic, strong) PywPayPwdSetProblemViewCell *cell;

@property (nonatomic, copy) NSArray     *titleArray;        // 问题标题数组
@property (nonatomic, copy) NSArray     *placeholderArray;  // 占位字符数组
@property (nonatomic, assign) NSInteger randomNum;          // 随机数存储

@end

@implementation PywValidProblemViewController
KeyBoardHelper(80)

- (void)viewDidLoad {
    [super viewDidLoad];
    self.title = @"验证安全问题";
    self.navigationItem.rightBarButtonItem = nil;
    
    [self setUp];
}
#pragma mark - prepareUI
- (void)setUp{
    [self.view addSubview:self.tableView];
    
    __weak typeof(self) wSelf = self;
    [self.tableView mas_makeConstraints:^(PywMASConstraintMaker *make) {
        make.top.bottom.left.right.offset(0);
    }];
    
}
#pragma mark - PywValidProblemViewDelegate
- (void)validProblemView:(PywValidProblemHeaderView *)validProblemView didSelectedCutButton:(UIButton *)cutBtn{
    self.cell.answer = @"";
    
    while (1) {
        NSInteger randomNum = arc4random() % 4;
        if (self.randomNum != randomNum) {
            self.randomNum = randomNum;
            
            if (self.randomNum == 0) {
                self.cell.answerIsDatePicker = YES;
                self.cell.placeholder = self.placeholderArray[0];
            } else {
                self.cell.answerIsDatePicker = NO;
                self.cell.placeholder = self.placeholderArray[1];
            }
            
            break;
        }
    }
    [self.tableView reloadData];
    PLog(@"点击切换问题============%d",self.randomNum);
}
#pragma mark - PywPayPwdFooterViewDelegate
- (void)payPwdFooterView:(PywPayPwdFooterView *)footerView didSelectedButton:(UIButton *)setButton{
    
    PLog(@"下一步  验证答案 %@",self.cell.answer);
    PywResetNewTradePwdController *resetNewTradePwdVC = [[PywResetNewTradePwdController alloc] init];
    [self.navigationController pushViewController:resetNewTradePwdVC animated:YES];
}
#pragma mark - UITableViewDelegate & UITableViewDataSource
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return 1;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    static NSString *identifier = @"cell";
    
    PywPayPwdSetProblemViewCell *cell = [tableView dequeueReusableCellWithIdentifier:identifier];
    if (!cell) {
        cell = [[PywPayPwdSetProblemViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:identifier];
        cell.delegate = self;
    }
    
    if (self.randomNum == indexPath.row && indexPath.row == 0) {
        cell.placeholder = self.placeholderArray[0];
        cell.answerIsDatePicker = YES;
    } else {
        cell.placeholder = self.placeholderArray[1];
        cell.answerIsDatePicker = NO;
    }
    
    cell.problem = self.titleArray[self.randomNum];
    self.cell = cell;
    
    return cell;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 44;
}

#pragma mark - Lazy
- (UITableView *)tableView{
    if (!_tableView) {
        _tableView = [[UITableView alloc] initWithFrame:CGRectZero style:UITableViewStylePlain];
        _tableView.backgroundColor = RGB(232, 232, 232, 1);
        _tableView.showsVerticalScrollIndicator = NO;
        _tableView.showsHorizontalScrollIndicator = NO;
        _tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
        
        PywValidProblemHeaderView *headerView = [[PywValidProblemHeaderView alloc] initWithFrame:CGRectMake(0, 0, 0, 80) Delegate:self];
        _tableView.tableHeaderView = headerView;
        
        PywPayPwdFooterView *footerView = [[PywPayPwdFooterView alloc] initWithFrame:CGRectMake(0, 0, 0, 100) Delegate:self ButtonTitle:@"下一步"];
        _tableView.tableFooterView = footerView;
        
        _tableView.delegate = self;
        _tableView.dataSource = self;
    }
    return _tableView;
}
#pragma mark - Setter & Getter
- (NSArray *)titleArray{
    if (!_titleArray) {
        _titleArray = @[@"我的生日",@"我的家乡",@"小学校名",@"父亲名字"];
    }
    return _titleArray;
}
- (NSArray *)placeholderArray{
    if (!_placeholderArray) {
        _placeholderArray = @[@"请选择日期",@"请输入答案"];
    }
    return _placeholderArray;
}


@end
